"""
试验流程控制（计时、成功判断、流程切换）
"""
import math
import time


class TrialManager:
    def __init__(self, time_limit=20):
        self.time_limit = time_limit  # 单次试验超时时间（秒）
        self.task_start_time = time.time()  # 当前试验开始时间
        self.current_trial_num = -1  # 当前试验编号
        self.success_count = 0

    def check_timeout(self):
        """检查试验是否超时"""
        elapsed = time.time() - self.task_start_time
        return elapsed > self.time_limit

    def check_success(self, cursor_pos, target_pos, r1=5, r2=100):
        """判断光标是否到达目标区域"""
        x1, y1 = cursor_pos
        x2, y2 = target_pos
        # distance = math.hypot(x2 - x1, y2 - y1)
        dis = (abs(x2 - x1) ** 2 + abs(y2 - y1) ** 2) ** 0.5

        if dis < r2 - r1 * 5:
            self.success_count +=1
            return 1
        else:
            return 0
        # return distance < (100 - 35*5)  # 目标半径 - 光标半径*系数

    def next_trial(self):
        """进入下一次试验"""
        self.current_trial_num += 1
        self.task_start_time = time.time()  # 重置计时器
        return self.current_trial_num

    def get_current_trial_num(self):
        """获取当前试验编号"""
        return self.current_trial_num

    def get_success_count(self):
        return self.success_count